
//
//  MLPictureView.swift
//  MLWeiBo
//
//  Created by wenxing on 2017/1/9.
//  Copyright © 2017年 Se7venX. All rights reserved.
//

import UIKit


private let pictureID = "pictureID"

class MLPictureView: UICollectionView {

    // 图片数组
    var pictureInfos:[MLPictureInfo]? {
    
        didSet{
        
          reloadData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.dataSource = self
        
        let nib = UINib(nibName: "MLPictureCell", bundle: nil)
        self.register(nib, forCellWithReuseIdentifier: pictureID)
    }
    
    
    

}

//MARK:- UICollectionViewDataSource -
extension MLPictureView:UICollectionViewDataSource {
    
    override var numberOfSections: Int {
    
      return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return pictureInfos?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: pictureID, for: indexPath) as! MLPictureCell

        cell.backgroundColor = RandomColor()
    
        let pictureDto = pictureInfos![indexPath.row] 
        
        let url = URL(string: pictureDto.thumbnail_pic ?? "")
        
        cell.pictureV.sd_setImage(with:url!, placeholderImage: #imageLiteral(resourceName: "avatar_default"))
        
        return cell
    }
    
    

//    @available(iOS 6.0, *)
//    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
//    
//    
//    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
//    @available(iOS 6.0, *)
//    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
//    
//    
//    @available(iOS 6.0, *)
//    optional public func numberOfSections(in collectionView: UICollectionView) -> Int

    

}
